package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.BasicInfosBean;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.viewholders.DpatientInfosViewholder;

import java.util.List;

/**
 * Created by william_sun on 2016-10-13.
 * Description:医生端查看的患者基本信息列表
 */
public class DpatientInfosAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<BasicInfosBean> datas;
    private LayoutInflater mInflater;
    private onBasicInfoTitleClickListener listener;

    public DpatientInfosAdapter(Context mContext, List<BasicInfosBean> datas, onBasicInfoTitleClickListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);
        this.listener = listener;
    }

    public void setData(List<BasicInfosBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_dpatientinfos, null);
        DpatientInfosViewholder viewholder = new DpatientInfosViewholder(view);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DpatientInfosViewholder viewholder = (DpatientInfosViewholder) holder;
        BasicInfosBean data = datas.get(position);
        viewholder.ll_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onTitleClick(position);
            }
        });
        viewholder.tv_date.setText(data.getDate().split(" ")[0]);
        if (data.isInfoShow()) {
            viewholder.iv_arrow.setImageResource(R.mipmap.tab_arrow_up);
            viewholder.ll_infos.setVisibility(View.VISIBLE);
        } else {
            viewholder.iv_arrow.setImageResource(R.mipmap.tab_arrow);
            viewholder.ll_infos.setVisibility(View.GONE);
        }
        viewholder.ll_infos.removeAllViews();
        for (BasicInfosBean.BasicInfosInfo info : data.getInfos()) {
            View view = new View(mContext);
            LinearLayout.LayoutParams lp_view = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ImageUtil.dip2px(3, mContext));
            lp_view.topMargin = ImageUtil.dip2px(8, mContext);
            lp_view.bottomMargin = ImageUtil.dip2px(8, mContext);
            view.setLayoutParams(lp_view);
            view.setBackgroundResource(R.drawable.divider_dash);
            view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速

            TextView tv_name = new TextView(mContext);
            LinearLayout.LayoutParams lp_tv1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            tv_name.setLayoutParams(lp_tv1);
            tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
            tv_name.setText("【" + info.getName() + "】");

            LinearLayout.LayoutParams lp_tv2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp_tv2.topMargin = ImageUtil.dip2px(10, mContext);
            TextView tv_info = new TextView(mContext);
            tv_info.setLayoutParams(lp_tv2);
            tv_info.setLineSpacing(ImageUtil.dip2px(5, mContext), 1);
            tv_info.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
            tv_info.setText(info.getInfo());

            viewholder.ll_infos.addView(view);
            if (!TextUtils.isEmpty(info.getName())) {
                viewholder.ll_infos.addView(tv_name);
            }
            viewholder.ll_infos.addView(tv_info);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface onBasicInfoTitleClickListener {
        void onTitleClick(int position);
    }
}
